package com.gaoyi.crm.repository;

import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.apiresult.PagedList;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.constant.DbMsg;
import com.gaoyi.crm.db.dao.CustomerDao;
import com.gaoyi.crm.db.dto.ShortDto;
import com.gaoyi.crm.db.dto.SyncedCustomerDto;
import com.gaoyi.crm.db.entity.Customer;
import com.gaoyi.crm.db.query.CtmSyncedQuery;
import com.gaoyi.crm.repository.base.BaseRepository;
import com.gaoyi.crm.util.OKHttpUtil;
import com.gaoyi.crm.util.ThreadManager;
import com.gaoyi.crm.util.Utils;

import java.util.Date;
import java.util.List;

public class CustomerRepository extends BaseRepository<Customer, CustomerDao> {
    @Override
    protected int getIndex() {
        return 0;
    }

    @Override
    protected CustomerDao getDao() {
        return mDatabase.getCustomerDao();
    }

    public void getShortCustomer(OnResultListener onResultListener){
        ThreadManager.getInstance().execute(()->{
            List<ShortDto> list = mDao.getShortCustomer(mDao.getShortCustomerSql(Utils.getName()));
            ApiResultData<List<ShortDto>> result = new ApiResultData<>();
            result.setStatus(ApiStatus.SUCCESS);
            result.setMessage(DbMsg.QUERY_SUCCESS);
            result.setData(list);
            OKHttpUtil.getInstance().post(()->onResultListener.onSuccess(result));
        });
    }

    public void getSyncedCustomer(CtmSyncedQuery query, OnResultListener onResultListener){
        ThreadManager.getInstance().execute(()->{
//            List<SyncedCustomerDto> customer = mDao.getCustomer(query.getKeyWord(), query.getStartDate(), query.getEndDate()
//                    , query.getOrderBySyncedAt(), query.getOrderBySyncStatus(), Utils.getOffset(query.getPageIndex()), Utils.getLimit());
//            int count = mDao.getCustomerCount(query.getKeyWord(), query.getStartDate(), query.getEndDate());
            String name = Utils.getName();
            List<SyncedCustomerDto> customer = mDao.getCustomer(mDao.getCustomerSql(name ,query,Utils.getOffset(query.getPageIndex()), Utils.getLimit()));
            int count = mDao.getCustomerCount(mDao.getCustomerSql(name,query));
            int totalPages = (count / 10) + 1;
            ApiResultData<Object> result = new ApiResultData<>();
            result.setStatus(ApiStatus.SUCCESS);
            result.setMessage(DbMsg.QUERY_SUCCESS);
            PagedList<SyncedCustomerDto> pagedList = new PagedList<>();
            pagedList.setPageIndex(query.getPageIndex());
            pagedList.setTotalPages(totalPages);
            pagedList.setList(customer);
            pagedList.setTotalCount(count);
            result.setData(pagedList);
            OKHttpUtil.getInstance().post(()->onResultListener.onSuccess(result));
        });
    }

    public int insertCustomers(List<Customer> customers, TableVersionRepository tableVersionRep) {
        if (customers == null || customers.isEmpty()) {
            return 0;
        }
        int ver = 0;
        for (int i = 0; i < customers.size(); i++) {
            customers.get(i).setSyncedAt(new Date());
            customers.get(i).setSyncedStatus(ApiStatus.DOWNLOAD_SUCCESS);
            int synVer = customers.get(i).getSynVer();
            if (ver < synVer) {
                ver = synVer;
            }
        }
        updateTableVersion(tableVersionRep, ver);
        mDao.inserts(customers);
        return customers.size();
    }
}
